package article

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"qycms/controllers"
	"qycms/models/article"
	"qycms/untils"
	"strings"
)

// 栏目
type CateController struct {
	controllers.BaseController
}

//新闻
type NewsController struct {
	controllers.BaseController
}

//文章列表页
func (this *NewsController) Index(){
	this.TplName = "article/index.html"
}

//文章列表
func (this *NewsController) NewList(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	news := []article.News{}

	O := orm.NewOrm()
	q := O.QueryTable(new(article.News))

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("title__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-id").RelatedSel().All(&news)

	this.Data["json"] = untils.ListJson(count,news)
	this.ServeJSON()
	this.StopRun()
}

//新建文章页面
func (this *NewsController) AddNews(){
	o := orm.NewOrm()
	cate := []article.Cate{}
	o.QueryTable(new(article.Cate)).Filter("is_delete",0).All(&cate)
	this.Data["cate"] = cate
	this.TplName = "article/addNews.html"
}

//新建文章操作
func (this *NewsController) InsertNews(){
	//接收参数
	cate_id,_ := this.GetInt("cate_id")
	title:= this.GetString("title")
	mark:= this.GetString("mark")
	content:= this.GetString("content")
	is_top,_:= this.GetInt("is_top")
	is_active,_:= this.GetInt("is_active")


	//数据验证
	valid := validation.Validation{}
	valid.Required(cate_id, "cate_id").Message("请选择栏目")
	valid.Required(title, "title").Message("请输入标题")
	valid.Required(mark, "mark").Message("请输入简短介绍")
	valid.Required(content, "content").Message("请输入内容")

	fileurl := this.GetString("pic")
	pic := this.Base64Upload(fileurl)

	//处理图片
	content = this.ContentImg(content)

	//绑定到模型
	o := orm.NewOrm()
	cate := article.Cate{}
	o.QueryTable(new(article.Cate)).Filter("id",cate_id).One(&cate)
	news := article.News{
		Title: title,
		ShortTitle: this.GetString("short_title"),
		Url: this.GetString("url"),
		IsTop: is_top,
		Keys: this.GetString("keys"),
		PushTime: this.GetString("push_time"),
		Pic: pic,
		Mark: this.GetString("mark"),
		Content: content,
		IsActive:is_active,
		Cate:&cate,
	}

	_, err := o.Insert(&news)




	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}

//修改文章页面
func (this *NewsController) EditNews(){
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

	//栏目
	cate := []article.Cate{}
	o.QueryTable(new(article.Cate)).Filter("is_delete",0).All(&cate)

	news :=article.News{}
	o.QueryTable(new(article.News)).Filter("id",id).RelatedSel().One(&news);
	this.Data["cate"] = cate
	this.Data["news"] = news
	this.TplName = "article/editNews.html"
}

//修改文章操作
func(this *NewsController) UpdateNew() {
	//接收参数
	ID := this.GetString("Id")
	cate_id,_ := this.GetInt("cate_id")
	title:= this.GetString("title")
	mark:= this.GetString("mark")
	content:= this.GetString("content")
	is_top:= this.GetString("is_top")
	is_active:= this.GetString("is_active")

	//数据验证
	valid := validation.Validation{}
	valid.Required(cate_id, "cate_id").Message("请选择栏目")
	valid.Required(title, "title").Message("请输入标题")
	valid.Required(mark, "mark").Message("请输入简短介绍")
	valid.Required(content, "content").Message("请输入内容")

	//绑定到模型
	var new_open int
	if is_active == "on"{
		new_open = 1
	}else{
		new_open = 0
	}

	var new_is_top int
	if is_top == "on"{
		new_is_top = 1
	}else{
		new_is_top = 0
	}

	fileurl := this.GetString("pic")
	var pic string
	if len(fileurl) == 0 {
		pic = this.GetString("oldpic")
	}else{
		pic = this.Base64Upload(fileurl)
	}

	//绑定到模型
	o := orm.NewOrm()

	news := orm.Params{
		"title": title,
		"short_title": this.GetString("short_title"),
		"url": this.GetString("url"),
		"is_top": new_is_top,
		"keys":this.GetString("keys"),
		"push_time": this.GetString("push_time"),
		"pic": pic,
		"mark": mark,
		"content": content,
		"is_active": new_open,
		"cate_id":cate_id,
	}

	_, err := o.QueryTable(new(article.News)).Filter("id",ID).Update(news)

	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}

//删除文章
func (this *NewsController) DelNew(){
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable(new(article.News))
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}





//栏目列表表
func (this *CateController) Cate(){
	this.TplName = "article/cate.html"
}

//栏目列表数据
func (this *CateController) CateList(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	cate := []article.Cate{}

	O := orm.NewOrm()
	q := O.QueryTable(new(article.Cate))

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("cate_name__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-id").All(&cate)

	this.Data["json"] = untils.ListJson(count,cate)
	this.ServeJSON()
	this.StopRun()
}

//新建栏目页
func (this *CateController) AddCate(){
	this.TplName = "article/addCate.html"
}

//新建栏目操作
func (this *CateController) InsertCate(){
	//接收参数
	cate_name:= this.GetString("cate_name")
	sort,_ := this.GetInt("sort")
	is_active,_:= this.GetInt("is_active")

	//数据验证
	valid := validation.Validation{}
	valid.Required(cate_name, "cate_name").Message("请输入栏目名称")
	valid.Required(sort, "sort").Message("请输入排序")
	valid.Required(is_active, "is_active").Message("请选择是否开启")

	//绑定到模型
	o := orm.NewOrm()
	cate := article.Cate{
		CateName: cate_name,
		Sort: sort,
		IsActive:is_active,
	}

	_, err := o.Insert(&cate)

	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}

//修改栏目
func (this *CateController) EditCate(){
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

	cate := article.Cate{}
	o.QueryTable(new(article.Cate)).Filter("id",id).Filter("is_delete",0).One(&cate)

	this.Data["cate"] = cate
	this.TplName = "article/editCate.html"
}

//修改栏目操作
func (this *CateController) UpdateCate(){
	//接收参数
	ID := this.GetString("Id")
	cate_name:= this.GetString("cate_name")
	sort,_ := this.GetInt("sort")
	is_active:= this.GetString("is_active")

	//数据验证
	valid := validation.Validation{}
	valid.Required(ID, "ID").Message("请传入栏目ID")
	valid.Required(cate_name, "cate_name").Message("请输入栏目名称")
	valid.Required(sort, "sort").Message("请输入排序")
	valid.Required(is_active, "is_active").Message("请选择是否开启")

	//绑定到模型
	o := orm.NewOrm()
	//绑定到模型
	var new_is_active int
	if is_active == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	group := orm.Params{
		"cate_name": cate_name,
		"sort": sort,
		"is_active": new_is_active,
	}

	_ ,err := o.QueryTable(new(article.Cate)).Filter("id",ID).Update(group)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//删除栏目
func(this *CateController) DelAction(){
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable(new(article.Cate))
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}